<%@ page import="org.apache.commons.fileupload.FileItem" %>
    <%@ page import="java.util.List" %>
    <%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
    <%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
    <%@ page import="org.apache.commons.io.FilenameUtils" %>
    <%@ page import="java.io.File" %>
    
    
    <%@ page import="java.awt.image.BufferedImage" %>
    <%@ page import="java.io.*" %>
    <%@ page import="javax.imageio.ImageIO" %>
    <%@ page import="org.apache.commons.fileupload.FileItem" %>
    <%@ page import="org.apache.commons.fileupload.FileUploadException" %>
    <%@ page import="org.imgscalr.Scalr" %>
    <%@ page import="net.sf.json.JSONArray" %>
    <%@ page import="net.sf.json.JSONObject" %>
    <%@ page contentType="text/html;charset=GB2312" language="java"%>

<%!
    

    private String getSuffix(String filename) {
	  String suffix = "";
	  int pos = filename.lastIndexOf('.');
	  if (pos > 0 && pos < filename.length() - 1) {
	      suffix = filename.substring(pos + 1);
	  }
	  return suffix;
    }
      
      
%>
<%!
    private String getMimeType(File file){
    String mimetype = "";
    if (file.exists()) {
	if (getSuffix(file.getName()).equalsIgnoreCase("png")) {
	    mimetype = "image/png";
	}else if(getSuffix(file.getName()).equalsIgnoreCase("jpg")){
	    mimetype = "image/jpg";
	}else if(getSuffix(file.getName()).equalsIgnoreCase("jpeg")){
	    mimetype = "image/jpeg";
	}else if(getSuffix(file.getName()).equalsIgnoreCase("gif")){
	    mimetype = "image/gif";
	}else {
                javax.activation.MimetypesFileTypeMap mtMap = new javax.activation.MimetypesFileTypeMap();
                mimetype  = mtMap.getContentType(file);
	}
    }
    return mimetype;
      
  }

%>
    <%

if (request.getParameter("sid") != null && !request.getParameter("sid").isEmpty()) {
    
    String syscode = request.getParameter("sid");
    String folder=Integer.toString(Math.round(Float.valueOf(syscode)/1000)*1000);
    
     String	clientPath = "papers/" +folder+"/"+ syscode+".jpg";     
	File file = new File(application.getRealPath(clientPath));

    //    PrintWriter writer = response.getWriter();
    //writer.write(clientPath);
	if (!file.exists()){
	    clientPath = "papers/blank.jpg";
	     file = new File(application.getRealPath(clientPath));
	}
	
	
    if (1==1 && file.exists()) {
	int bytes = 0;
	ServletOutputStream op = response.getOutputStream();

	response.setContentType(getMimeType(file));
	response.setContentLength((int) file.length());
	response.setHeader( "Content-Disposition", "inline; filename=\"" + file.getName() + "\"" );

	byte[] bbuf = new byte[1024];
	DataInputStream in = new DataInputStream(new FileInputStream(file));

	while ((in != null) && ((bytes = in.read(bbuf)) != -1)) {
	    op.write(bbuf, 0, bytes);
	}

	in.close();
	op.flush();
	op.close();
    }

} else {
    PrintWriter writer = response.getWriter();
    writer.write("call POST with multipart form data");
}



 %>

